package com.jsb.laundry.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsb.laundry.common.exception.BusinessException;
import com.jsb.laundry.common.exception.ErrorCode;
import com.jsb.laundry.dao.JsbPostFavoritesMapper;
import com.jsb.laundry.model.entity.JsbPostFavorites;
import com.jsb.laundry.model.entity.JsbPosts;
import com.jsb.laundry.service.JsbPostFavoritesService;
import com.jsb.laundry.service.JsbPostsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
* @author ching
* @description 针对表【jsb_post_favorites(帖子收藏关系表)】的数据库操作Service实现
* @createDate 2023-08-09 14:06:04
*/
@Service
public class JsbPostFavoritesServiceImpl extends ServiceImpl<JsbPostFavoritesMapper, JsbPostFavorites>
    implements JsbPostFavoritesService {

    @Resource
    private JsbPostsService postService;

    /**
     * 帖子收藏
     *
     * @param postId
     * @param userId
     * @return
     */
    @Override
    public int doPostFavour(long postId, String userId) {
        // 判断是否存在
        JsbPosts post = postService.getById(postId);
        if (post == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        // 是否已帖子收藏
        // 每个用户串行帖子收藏
        // 锁必须要包裹住事务方法
        JsbPostFavoritesService postFavourService = (JsbPostFavoritesService) AopContext.currentProxy();
        synchronized (String.valueOf(userId).intern()) {
            return postFavourService.doPostFavourInner(userId, postId);
        }
    }

    @Override
    public Page<JsbPosts> listFavourPostByPage(IPage<JsbPosts> page, Wrapper<JsbPosts> queryWrapper, String favourUserId) {
        if (StringUtils.isAnyBlank(favourUserId)) {
            return new Page<>();
        }
        return baseMapper.listFavourPostByPage(page, queryWrapper, favourUserId);
    }
    /**
     * 封装了事务的方法
     *
     * @param userId
     * @param postId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int doPostFavourInner(String userId, long postId) {
        JsbPostFavorites postFavour = new JsbPostFavorites();
        postFavour.setUserId(userId);
        postFavour.setPostId(postId);
        QueryWrapper<JsbPostFavorites> postFavourQueryWrapper = new QueryWrapper<>(postFavour);
        JsbPostFavorites oldPostFavour = this.getOne(postFavourQueryWrapper);
        boolean result;
        // 已收藏
        if (oldPostFavour != null) {
            result = this.remove(postFavourQueryWrapper);
            if (result) {
                // 帖子收藏数 - 1
                result = postService.update()
                        .eq("id", postId)
                        .gt("favorites", 0)
                        .setSql("favorites = favorites - 1")
                        .update();
                return result ? -1 : 0;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        } else {
            // 未帖子收藏
            result = this.save(postFavour);
            if (result) {
                // 帖子收藏数 + 1
                result = postService.update()
                        .eq("id", postId)
                        .setSql("favorites = favorites + 1")
                        .update();
                return result ? 1 : 0;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        }
    }
}




